const dialog = require("../sdk/DIALOG")


module.exports = function (data) {
  console.log(data)
  //首先解析数据
  let xml = (
    <frame margin='8dp'>
      <card
        cardBackgroundColor="#ffffff"
        cardCornerRadius='10dp'
        cardElevation='2dp'
        foreground="?selectableItemBackground"
        w='*'
        h='auto'
      >
      <vertical>
        <horizontal w='*' bg={this.app.color} gravity='center' padding='5dp'>
          <text textColor={this.app.font_color} text={data.title} textSize='16sp'></text>
        </horizontal>
        <frame padding='5dp'>
          {show_from.call(this)}
        </frame>
      </vertical>
    </card>
  </frame>
  )

  //定义函数
  function show_from() {

    let cview = (<text w='0' h='0'></text>),
    i = 0
    for (v in data.views) {
      i++
      console.log(v)
      switch (v) {
        case "spinner":

          if (!data.views[v].id) {
            data.views[v].id = 'spinner_'+i.toString()
          }

          if (!data.views[v].spinnerMode) {
            data.views[v].spinnerMode = 'dropdown'
          }

          cview += (
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <spinner w='*' 
              entries={data.views[v].entries} 
              id={data.views[v].id}
              spinnerMode={data.views[v].spinnerMode}
              ></spinner>
            </linear>
          )
        break;
        case "input":
          if (!data.views[v].id) {
            data.views[v].id = 'input_'+i.toString()
          }

          if (!data.views[v].spinnerMode) {
            data.views[v].hint = ''
          }

          cview+=(
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <input w='*' hint={data.views[v].hint} id={data.views[v].id}></input>
            </linear>
          )
        break;
        case "checkbox":
          let checkboxXml = (<text w='0' h='0'></text>)
          data.views.checkbox.arr.forEach((c, index) => {
            if (!c.able) {
              c.able = false
            }
            checkboxXml+=(
              <checkbox id={'checkbox_'+i.toString()+'_'+index} text={c.text} checked={c.able}></checkbox>
            )
          })

          cview+=(
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <horizontal gravity='center_vertical'>
                {checkboxXml}
              </horizontal>
            </linear>
          )
        break;
        case "radio":
          let radioXml = (<text w='0' h='0'></text>)
          data.views.radio.arr.forEach((c, index) => {
            if (!c.able) {
              c.able = false
            }
            radioXml+=(
              <radio id={'radio_'+i.toString()+'_'+index} text={c.text} checked={c.able}></radio>
            )
          })

          cview+=(
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <radiogroup gravity='center_vertical' orientation='horizontal'>
                {radioXml}
              </radiogroup>
            </linear>
          )
        break;
        case "switch":
          let switchXml = (<text w='0' h='0'></text>)
          if (!data.views[v].able) {
            data.views[v].able=false
          }

          if (!data.views[v].id) {
            data.views[v].id = 'switch_'+i.toString()

          }
          cview+=(
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <Switch text={data.views[v].table} 
              checked={data.views[v].able}
              id={data.views[v].id}
              ></Switch>
            </linear>
          )
        break;
        case "datepicker" :
          let datepickerXml = (<text w='0' h='0'></text>)
          if (!data.views[v].id) {
            data.views[v].id = 'datepicker_'+i.toString()
          }
          cview+=(
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <EditText hint={data.views[v].hint}
                focusable='false'
                enabled='true'     
                id='dddd'
                gravity='left'
                w='*'
              ></EditText>
            </linear>
          )
          ui.post(() => {
            ui.dddd.on('click', () => {

              dialog.datePicker((r) => {
                // console.log(r)
                let rq = r.y+'-'+r.m+'-'+r.d
                ui.dddd.setText(rq)
              })

            })
          }, 0)
        break;
        case 'timepicker':
          console.log("v__>>"+data.views[v].table)
          // let timepickerXml = (<text w='0' h='0'></text>)
          if (!data.views[v].id) {
            data.views[v] = 'timepicker_'+i.toString()
          }

          cview += (
            <linear w='*' gravity='center_vertical' orientation={data.orientation}>
              <text text={data.views[v].table}></text>
              <EditText hint={data.views[v].hint}
                focusable='false'
                enabled='true'     
                id='bbbbb'
                gravity='left'
                w='*'
              ></EditText>

            </linear>
          )
          //创建操作事件
          
          ui.post(() => {
            ui.bbbbb.on('click', () => {
              dialog.timePicker()
            })
          }, 0)

        break;
        
      }
    }

    let xml = (
      <linear orientation='vertical' w='*'>
        {cview}
      </linear>
    )
  
    return xml
  }
  //然后注册组件
  return xml
}